package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLNonTransientException;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void deleteById(Long id) {
        //1. 根据分类id查询菜品表数据 select count(*) from dish where category_id=?
        Integer count1 = dishService.query()
                .eq("category_id", id)
                .count();
        if (count1 > 0) {
            //有关联菜品数据，不能删除，抛出异常
            throw new CustomerException("该分类有对应的菜品数据，不能删除！");
        }

        //2. 根据分类id查询套餐表数据 select count(*) from setmeal where category_id=?
        Integer count2 = setmealService.query()
                .eq("category_id", id)
                .count();
        if (count2 > 0) {
            //有关联数据，不能删除
            throw new CustomerException("该分类有对应的套餐数据，不能删除！");
        }

        //3. 根据id删除分类表数据 delete from category where id=?
        this.removeById(id);

    }
}
